package app.android.com.wuandroidapp.test.presenter;

import android.content.Context;
import android.widget.Toast;

import javax.inject.Inject;

import app.android.com.wuandroidapp.library.util.RxSchedulerUtils;
import app.android.com.wuandroidapp.test.contract.HomePageContract;
import app.android.com.wuandroidapp.test.model.repository.WeatherDataRepository;
import app.android.com.wuandroidapp.test.util.ActivityScoped;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;
import rx.subscriptions.Subscriptions;


/**
 * @author baronzhang (baron[dot]zhanglei[at]gmail[dot]com ==>> baronzhang.com)
 */
@ActivityScoped
public final class HomePagePresenter implements HomePageContract.Presenter {

    private final Context context;
    private final HomePageContract.View weatherView;
    private CompositeSubscription subscriptions;
    @Inject
    HomePagePresenter(Context context, HomePageContract.View view) {
        this.context = context;
        this.weatherView = view;
        weatherView.setPresenter(this);
        this.subscriptions = new CompositeSubscription();

    }

    @Override
    public void subscribe() {
        loadWeather();
    }

    @Override
    public void loadWeather() {
        Subscription subscription= WeatherDataRepository.getWeather(context).compose(RxSchedulerUtils.normalSchedulersTransformer())
                .subscribe(weatherView::displayWeatherInformation, throwable -> {
                    Toast.makeText(context, throwable.getMessage(), Toast.LENGTH_LONG).show();
                });
        subscriptions.add(subscription);
    }

    @Override
    public void unSubscribe() {
        subscriptions.clear();
    }
}
